package com.ITXu.reggie.controller;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ITXu.reggie.commom.BaseContext;
import com.ITXu.reggie.commom.R;
import com.ITXu.reggie.entity.ShoppingCart;
import com.ITXu.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/shoppingCart")
@CrossOrigin
@Slf4j
public class ShoppingCartController {

	 @Autowired
	 private ShoppingCartService shoppingCartService;
	 
	 /**
	  * 新增shoppingCart
	  * @param shoppingCart
	  * @return
	  */
	 @PostMapping("/add")
	 public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart) {
		 //取得userId放入shoppingcart
		 Long userId = BaseContext.getCurrentId();
		 shoppingCart.setUserId(userId);
		 shoppingCart.setCreateTime(LocalDateTime.now());
		 LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(ShoppingCart::getUserId, userId);
		 //根据userId，dishId或setmealId查询数据库取出对应的shoppingcart对象
		 if (shoppingCart.getDishId() != null) {
			queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
		} else {
			queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
		}
		 ShoppingCart Cart = shoppingCartService.getOne(queryWrapper);
		 //如果该shoppingcart对象为空，执行新增；如果不为空，设置number为number+1，执行更新
		 if (Cart == null) {
			shoppingCart.setNumber(1);
			shoppingCartService.save(shoppingCart);
			Cart = shoppingCart;
		}
		 else {
			Integer num = Cart.getNumber();
			Cart.setNumber(num+1);
			shoppingCartService.updateById(Cart);
		}
		 return R.success(Cart);
	 }
	 
	 /**
	  * 减去一个当前购物车对象
	  * @param shoppingCart
	  * @return
	  */
	 @PostMapping("/sub")
	 public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
		 //根据userId和setmealId（或dishId）查找到对应的shoppingCart数据，把该对象的number-1，后执行更新操作
		 LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
		 if (shoppingCart.getDishId() != null) {
			queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
		} else {
			queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
		}
		 ShoppingCart subCart = shoppingCartService.getOne(queryWrapper);
		 Integer num = subCart.getNumber();
		 if (num >= 1) {
			 subCart.setNumber(num-1);
			 if (num-1==0) {
				this.clean();
			} else {
				shoppingCartService.updateById(subCart);
				 return R.success(subCart);
			}
		}
		 return R.error("当前菜品或套餐为空");
	 } 
	 
	 /**
	  * 查询购物车列表
	  * @return
	  */
	 @GetMapping("/list")
	 public R<List<ShoppingCart>> list() {
		 LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
		 queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
		 List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
		 return R.success(list);
	 }
	 
	 @DeleteMapping("/clean")
	 public R<String> clean(){
		 LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
		 shoppingCartService.remove(queryWrapper);
		 return R.success("清空购物车成功");
	 }
}
